package cn.jtfadmin.base.data.jpa.domain.entity;

import cn.jtfadmin.base.lang.common.domain.entity.CUDateUserEntity;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 基础实体，定义id创建人创建时间，更新人，更新时间
 */
@MappedSuperclass
@Getter
@Setter
@ToString()
@Table(indexes = {
        @Index(columnList = "create_date_time"),
        @Index(columnList = "update_date_time")
})
@EntityListeners(value = DefaultEntityListener.class)
public class BaseEntity implements Serializable, CUDateUserEntity {


    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /**
     * id 实体唯一标志
     */
    @Id
    @Column(name = "id", length = 100)
    protected String id;

    /**
     * 创建时间
     */
    @Column(name = "create_date_time", updatable = false)
    protected LocalDateTime createDateTime;

    /**
     * 修改时间
     */
    @Column(name = "update_date_time")
    protected LocalDateTime updateDateTime;

    /**
     * 创建者用户id
     */
    @Column(name = "create_user_id", length = 100)
    protected String createUserId;


    /**
     * 更新者用户id
     */
    @Column(name = "update_user_id", length = 100)
    protected String updateUserId;

    /**
     * 版本
     */
    @Version
    @Column(name = "version")
    protected Long version;


    public String getId() {
        if (StringUtils.hasText(id)) {
            return id;
        }
        return null;
    }

    @Override
    public boolean equals(Object obj) {
        if (Objects.isNull(obj)) {
            return false;
        }
        if (obj.getClass() == getClass()) {
            return Objects.equals(getId(), ((BaseEntity) obj).getId());
        } else {
            return false;
        }
    }


    @Override
    public int hashCode() {
        return Objects.hashCode(getId());
    }
}
